package com.xmall.order.controller;

import com.xmall.aspect.OperateRecord;
import com.xmall.aspect.RepeatSubmit;
import com.xmall.define.base.Result;
import com.xmall.define.dict.OperateTypeEnum;
import com.xmall.order.define.dto.OrderExpressDto;
import com.xmall.order.define.request.express.OrderExpressUpdateRequest;
import com.xmall.order.service.IOrderExpressService;
import com.xmall.validate.group.AddGroup;
import com.xmall.validate.group.UpdateGroup;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 订单物流
 */
@RequestMapping("/service/order/express")
@RestController
public class OrderExpressController {

    @Resource
    private IOrderExpressService orderExpressService;

    /**
     * 根据订单ID查询详情
     */
    @GetMapping("infoByOrderId/{orderId}")
    public Result<OrderExpressDto> infoByOrderId(@PathVariable("orderId") Long orderId){
        OrderExpressDto orderExpressDto = orderExpressService.infoByOrderId(orderId);
        return Result.success(orderExpressDto);
    }

    /**
     * 发货
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @RepeatSubmit
    @PreAuthorize("hasAnyAuthority('order:order:delivery')")
    @PostMapping("/consignOrder")
    public Result consignOrder(@Validated(value = AddGroup.class) @RequestBody OrderExpressUpdateRequest request){
        orderExpressService.consignOrder(request);
        return Result.success();
    }

    /**
     * 编辑
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @PreAuthorize("hasAnyAuthority('order:order:delivery')")
    @PostMapping("/update")
    public Result update(@Validated(value = UpdateGroup.class) @RequestBody OrderExpressUpdateRequest request){
        orderExpressService.update(request);
        return Result.success();
    }

}
